package com.schedule.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.schedule.dao.BusDao;
import com.schedule.model.Bus;
import com.schedule.model.Place;


@Controller
@RequestMapping("/bus")
public class BusController {
	@Autowired
	private BusDao busDao;
	
	@RequestMapping(method=RequestMethod.GET)
	public ModelAndView getAll(
			HttpSession session
			){
		ModelAndView mav=new ModelAndView("bus/BusList");
		List<Bus> buses=busDao.findAll();
		System.out.println(buses.size());
		mav.addObject("buses",buses);
		 String adminName=session.getAttribute("adminName").toString(); 
		 mav.addObject("name",adminName);
		return mav;
	}
	
	@RequestMapping(value="/delete",method=RequestMethod.GET)
	public ModelAndView delete(
			@RequestParam String busId
			){
			busDao.deleteBus(busId);
		ModelAndView mav=new ModelAndView("redirect:/bus");
		return mav;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.GET)
	public ModelAndView update(
			@RequestParam String busId
			){
		Bus bus=busDao.findById(busId);
		ModelAndView mav=new ModelAndView("bus/updateBus");
		mav.addObject(bus);
		return mav;
	}
	
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public ModelAndView update(
			@RequestParam String bId,
			@RequestParam String num
			){
		Bus bus=new Bus();
		bus.setBusId(bId);
		bus.setNum(Integer.parseInt(num));
		busDao.updateBus(bus);
		ModelAndView mav=new ModelAndView("redirect:/bus");
		return mav;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.GET)
	public ModelAndView add(){
		ModelAndView mav=new ModelAndView("bus/addBus");
		return mav;
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public ModelAndView add(
			@RequestParam String busId,
			@RequestParam int num
			){
		Bus bus=new Bus();
		bus.setBusId(busId);
		bus.setNum(num);
		busDao.addBus(bus);
		ModelAndView mav=new ModelAndView("redirect:/bus");
		return mav;
	}
}
